/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.googlecode.datawander.Presenter;

import com.googlecode.datawander.View.ConnectionValues;
import com.googlecode.datawander.i18n.I18n;

import org.apache.log4j.Logger;

/**
 *
 * @author Marcin Stachniuk
 */
public class DataWanderPresenter implements DataWanderPresenterInt {

    private boolean isMigrationInProgress = false;
    private static final Logger logger = Logger.getLogger(DataWanderPresenter.class);
    private DataWanderViewInt dataWanderView;
    private Relational2ObjectMigrationThread relational2Object;
    private Object2RelationalMigrationThread object2Relational;


    public void relational2ObjectMigration(ConnectionValues connectionValues) 
            throws ConnectionValuesException {
        logger.trace("relational2ObjectMigration()");

        if(isMigrationInProgress()) {
            dataWanderView.enterLog(I18n.getString("migrationisinprogress"));
            dataWanderView.showErrorMessage();
        } else {
            testConnectionValuesRel2ObjectMigration(connectionValues);
            dataWanderView.disableInputsControls();
            isMigrationInProgress = true;
            relational2Object = new Relational2ObjectMigrationThread();
            relational2Object.setDataWanderView(dataWanderView);
            relational2Object.setDataWanderPresenter(this);
            relational2Object.setConnectionValues(connectionValues);
            relational2Object.start();
        }
    }

    public void object2RelationalMigration(ConnectionValues connectionValues) 
            throws ConnectionValuesException {
        logger.trace("object2RelationalMigration()");

        if(isMigrationInProgress()) {
            dataWanderView.enterLog(I18n.getString("migrationisinprogress"));
            dataWanderView.showErrorMessage();
        } else {
            testConnectionValuesObj2RelationalMigration(connectionValues);
            dataWanderView.disableInputsControls();
            isMigrationInProgress = true;
            object2Relational = new Object2RelationalMigrationThread();
            object2Relational.setDataWanderView(dataWanderView);
            object2Relational.setDataWanderPresenter(this);
            object2Relational.setConnectionValues(connectionValues);
            object2Relational.start();
        }
    }

    public boolean isMigrationInProgress() {
        return isMigrationInProgress;
    }

    public void setDataWanderView(DataWanderViewInt dataWanderView) {
        this.dataWanderView = dataWanderView;
    }

    public void endMigrationProcess() {
        isMigrationInProgress = false;
        dataWanderView.enableInputsControls();
    }

    private void testConnectionValuesRel2ObjectMigration(
            ConnectionValues connectionValues) throws ConnectionValuesException {
        String message;
        if(connectionValues.getObjectFilename() == null ||
                connectionValues.getObjectFilename().length() == 0) {
            message = I18n.getString("nochosefiletodb4odatabase");
            throw new ConnectionValuesException(message);
        }
        
        if(connectionValues.getRelationalURL() == null ||
                connectionValues.getRelationalURL().length() == 0) {
            message = I18n.getString("nochoserelationalurl");
            throw new ConnectionValuesException(message);
        }

        if(connectionValues.getRelationalUsername() == null ||
                connectionValues.getRelationalUsername().length() == 0) {
            message = I18n.getString("nochoserelationaluser");
            throw new ConnectionValuesException(message);
        }

        if(connectionValues.getRelationalPassword() == null ||
                connectionValues.getRelationalPassword().length() == 0) {
            message = I18n.getString("nochoserelationalpassword");
            throw new ConnectionValuesException(message);
        }
    }

    private void testConnectionValuesObj2RelationalMigration (
            ConnectionValues connectionValues) throws ConnectionValuesException {
        testConnectionValuesRel2ObjectMigration(connectionValues);

        if(connectionValues.getObjectJarOrCompiledFolder() == null ||
                connectionValues.getObjectJarOrCompiledFolder().length() == 0) {
            String message = I18n.getString("nochoseobjeccodefolder");
            throw new ConnectionValuesException(message);
        }
    }

}
